package com.jinxin.platform.cdsmsmgr.alisms;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2020-07-01
 * @Author: Lingnan
 **/

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.jinxin.platform.cdsmsmgr.pojo.entity.SignFile;
import com.jinxin.platform.cdsmsmgr.pojo.entity.SmsConstant;

import java.util.List;

/**
 * 阿里短信服务
 *
 *
 */
public class AliSms {

    private static String REGIN_ID_NAME = "RegionId";

    private static String REGIN_ID_VALUE = "cn-hangzhou";

    private static String DOMAIN = "dysmsapi.aliyuncs.com";

    private static String VERSION = "2017-05-25";

    private static String PHONE_NUMBERS = "PhoneNumbers";

    private static String SIGN_NAME = "SignName";

    private static String TEMPLATE_CODE = "TemplateCode";

    private static String TEMPLATE_PARAM = "TemplateParam";

    private static String SMS_UP_EXTEND_CODE = "SmsUpExtendCode";

    private static String OUT_ID = "OutId";

    private static String PHONE_NUMBER_JSON = "PhoneNumberJson";

    private static String SIGN_NAME_JSON = "SignNameJson";

    private static String TEMPLATE_PARAM_JSON = "TemplateParamJson";

    private static String SMS_UP_EXTEND_CODE_JSON = "SmsUpExtendCodeJson";

    private static String PHONE_NUMBER = "PhoneNumber";

    private static String SEND_DATE = "SendDate";

    private static String PAGE_SIZE = "PageSize";

    private static String PAGE_INDEX = "PageIndex";

    private static String CURRENT_PAGE = "CurrentPage";

    private static String BIZ_ID = "BizId";

    private static String TEMPLATE_TYPE = "TemplateType";

    private static String TEMPLATE_NAME = "TemplateName";

    private static String TEMPLATE_CONTENT = "TemplateContent";

    private static String SEND_SMS_ACTION = "SendSms";

    private static String SEND_BATCH_SMS_ACTION = "SendBatchSms";

    private static String QUERY_SEND_DETAILS_ACTION = "QuerySendDetails";

    private static String QUERY_SMS_SIGN_ACTION = "QuerySmsSign";

    private static String QUERY_SMS_SIGN_List_ACTION = "QuerySmsSignList";

    private static String QUERY_SMS_TEMPLATE_ACTION = "QuerySmsTemplate";

    private static String QUERY_SMS_TEMPLATE_LIST_ACTION = "QuerySmsTemplateList";

    private static String ADD_SMS_TEMPLATE_ACTION = "AddSmsTemplate";

    private static String MODIFY_SMS_TEMPLATE_ACTION = "ModifySmsTemplate";

    private static String DELETE_SMS_TEMPLATE_ACTION = "DeleteSmsTemplate";

    private static String ADD_SMS_SIGN_ACTION = "AddSmsSign";

    private static String MODIFY_SMS_SIGN_ACTION = "ModifySmsSign";

    private static String DELETE_SMS_SIGN_ACTION = "DeleteSmsSign";

    private static String SIGN_SOURCE = "SignSource";

    private static String SIGN_TYPE = "SignType";

    private static String REMARK = "Remark";

    private String accessKeyId;

    private String accessSecret;

    public AliSms() {
    }

    public AliSms(String accessKeyId, String accessSecert) {
        this.accessKeyId = accessKeyId;
        this.accessSecret = accessSecert;
    }

    /**
     * 短信发送,支持向多个不同的手机号码发送同样内容的短信
     * @param phoneNumbers 必填，接收短信的手机号码。
    格式：
    国内短信：11位手机号码，例如15951955195。
    国际/港澳台消息：国际区号+号码，例如85200000000。
    支持对多个手机号码发送短信，手机号码之间以英文逗号（,）分隔。上限为1000个手机号码。批量调用相对于单条调用及时性稍有延迟
     * @param signName 必填，短信签名名称
     * @param templateCode 必填，短信模板ID
     * @param templateParam 必填，短信模板变量对应的实际值，JSON格式
     * @param smsUpExtendCode 非必填，上行短信扩展码，无特殊需要此字段的用户请忽略此字段
     * @param outId 非必填，外部流水扩展字段
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse sendSms(String phoneNumbers, String signName, String templateCode, String templateParam,
                                  String smsUpExtendCode, String outId) throws ServerException, ClientException {
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(SEND_SMS_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(PHONE_NUMBERS, phoneNumbers);
        request.putQueryParameter(SIGN_NAME, signName);
        request.putQueryParameter(TEMPLATE_CODE, templateCode);
        request.putQueryParameter(TEMPLATE_PARAM, templateParam);
        request.putQueryParameter(SMS_UP_EXTEND_CODE, smsUpExtendCode);
        request.putQueryParameter(OUT_ID, outId);
        return client.getCommonResponse(request);
    }

    /**
     * 批量发送短信
     * @param phoneNumberJson 必填，接收短信的手机号码，数组格式["1590000****","1350000****"]。
     * @param signNameJson 必填，短信签名名称，数组格式["阿里云","阿里巴巴"]
     * @param templateCode 必填，短信模板CODE
     * @param templateParamJson 必填，短信模板变量对应的实际值，
     *                          数组格式[{"name":"TemplateParamJson"},{"name":"TemplateParamJson"}]
     * @param smsUpExtendCodeJson 非必填，上行短信扩展码，数组格式。["90999","90998"]无特殊需要此字段的用户请忽略此字段
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse sendBatchSms(String phoneNumberJson, String signNameJson, String templateCode,
                                       String templateParamJson, String smsUpExtendCodeJson) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(SEND_BATCH_SMS_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(PHONE_NUMBER_JSON, phoneNumberJson);
        request.putQueryParameter(SIGN_NAME_JSON, signNameJson);
        request.putQueryParameter(TEMPLATE_CODE, templateCode);
        request.putQueryParameter(TEMPLATE_PARAM_JSON, templateParamJson);
        request.putQueryParameter(SMS_UP_EXTEND_CODE_JSON, smsUpExtendCodeJson);
        return client.getCommonResponse(request);
    }

    /**
     * 查询某个手机号的短信发送情况。短信发送状态有：提交，成功，失败
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse querySendDetails(String phoneNumber, String sendDate, String pageSize, String currentPage,
                                           String bizId) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(QUERY_SEND_DETAILS_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(PHONE_NUMBER, phoneNumber);
        request.putQueryParameter(SEND_DATE, sendDate);
        request.putQueryParameter(PAGE_SIZE, pageSize);
        request.putQueryParameter(CURRENT_PAGE, currentPage);
        request.putQueryParameter(BIZ_ID, bizId);
        return client.getCommonResponse(request);
    }

    public CommonResponse querySmsTemplateList(int pageIndex,int pageSize) throws ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(QUERY_SMS_TEMPLATE_LIST_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(PAGE_INDEX, pageIndex+"");
        request.putQueryParameter(PAGE_SIZE, pageSize+"");
        return client.getCommonResponse(request);

    }

    /**
     * 查询短信模板的审核状态
     * @param templateCode 短信模板CODE
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse querySmsTemplate(String templateCode) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(QUERY_SMS_TEMPLATE_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(TEMPLATE_CODE, templateCode);
        return client.getCommonResponse(request);
    }

    /**
     * 修改短信模板
     * @param templateType 必填，短信类型。其中：
    0：验证码。
    1：短信通知。
    2：推广短信。
    3：国际/港澳台消息
     * @param templateName 必填，模板名称，长度为1~30个字符
     * @param templateContent 必填，模板内容，长度为1~500个字符
     * @param remark 必填，短信模板申请说明。请在申请说明中描述您的业务使用场景，长度为1~100个字符
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse modifySmsTemplate(String templateType, String templateName, String templateContent, String remark,String templateCode) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(MODIFY_SMS_TEMPLATE_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(TEMPLATE_TYPE, templateType);
        request.putQueryParameter(TEMPLATE_NAME, templateName);
        request.putQueryParameter(TEMPLATE_CONTENT, templateContent);
        request.putQueryParameter(REMARK, remark);
        request.putQueryParameter(TEMPLATE_CODE, templateCode);
        return client.getCommonResponse(request);
    }

    /**
     * 申请短信模板
     * @param templateType 必填，短信类型。其中：
    0：验证码。
    1：短信通知。
    2：推广短信。
    3：国际/港澳台消息
     * @param templateName 必填，模板名称，长度为1~30个字符
     * @param templateContent 必填，模板内容，长度为1~500个字符
     * @param remark 必填，短信模板申请说明。请在申请说明中描述您的业务使用场景，长度为1~100个字符
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse addSmsTemplate(String templateType, String templateName, String templateContent, String remark) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(ADD_SMS_TEMPLATE_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(TEMPLATE_TYPE, templateType);
        request.putQueryParameter(TEMPLATE_NAME, templateName);
        request.putQueryParameter(TEMPLATE_CONTENT, templateContent);
        request.putQueryParameter(REMARK, remark);
        return client.getCommonResponse(request);
    }

    /**
     * 删除短信模板
     * @param templateCode 短信模板CODE
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse deleteSmsTemplate(String templateCode) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(DELETE_SMS_TEMPLATE_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(TEMPLATE_CODE, templateCode);
        return client.getCommonResponse(request);
    }

    /**
     * 查询短信签名申请状态
     * @param signName 签名名称
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse querySmsSign(String signName) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(QUERY_SMS_SIGN_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(SIGN_NAME, signName);
        return client.getCommonResponse(request);
    }

    /**
     * 查询短信签名申请状态
     * @param pageIndex 展示第几页的签名信息。默认取值为1。
     * @param pageSize 每页展示的签名条数。取值范围：1~50。
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse querySmsSignList(int pageIndex,int pageSize) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(QUERY_SMS_SIGN_List_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(PAGE_INDEX, pageIndex+"");
        request.putQueryParameter(PAGE_SIZE, pageSize+"");
        return client.getCommonResponse(request);
    }

    /**
     * 申请短信签名
     * @param signName 必填，签名名称
     * @param signSource 必填，签名来源。其中：
    0：企事业单位的全称或简称。
    1：工信部备案网站的全称或简称。
    2：APP应用的全称或简称。
    3：公众号或小程序的全称或简称。
    4：电商平台店铺名的全称或简称。
    5：商标名的全称或简称
     * @param remark 必填，短信签名申请说明。请在申请说明中详细描述您的业务使用场景，申请工信部备案网站的全称或简称请在此处填写域名，长度不超过200个字符
     * @param signFileList 必填，签名的证明文件
    key: 签名的证明文件格式，支持上传多张图片。当前支持jpg、png、gif或jpeg格式的图片。个别场景下，申请签名需要上传证明文件
    value: 签名的资质证明文件经base64编码后的字符串。图片不超过2 MB。个别场景下，申请签名需要上传证明文件
     * @param signType signType 0：验证码 1：通用
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse addSmsSign(String signName, String signSource, String remark,
                                     List<SignFile> signFileList,String signType) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(ADD_SMS_SIGN_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(SIGN_NAME, signName);
        request.putQueryParameter(SIGN_SOURCE, signSource);
        request.putQueryParameter(REMARK, remark);
        request.putQueryParameter(SIGN_TYPE, signType);
        if(signFileList != null) {
            for(int i = 1;i <= signFileList.size();i++) {
                request.putQueryParameter("SignFileList." + i + ".FileSuffix", signFileList.get(i - 1).getFileSuffix());
                request.putQueryParameter("SignFileList." + i + ".FileContents", signFileList.get(i - 1).getFileContents());
            }
        }
        return client.getCommonResponse(request);
    }

    /**
     * 修改短信签名
     * @param signName 必填，签名名称
     * @param signSource 必填，签名来源。其中：
    0：企事业单位的全称或简称。
    1：工信部备案网站的全称或简称。
    2：APP应用的全称或简称。
    3：公众号或小程序的全称或简称。
    4：电商平台店铺名的全称或简称。
    5：商标名的全称或简称
     * @param remark 必填，短信签名申请说明。请在申请说明中详细描述您的业务使用场景，申请工信部备案网站的全称或简称请在此处填写域名，长度不超过200个字符
     * @param signFileList 必填，签名的证明文件
    key: 签名的证明文件格式，支持上传多张图片。当前支持jpg、png、gif或jpeg格式的图片。个别场景下，申请签名需要上传证明文件
    value: 签名的资质证明文件经base64编码后的字符串。图片不超过2 MB。个别场景下，申请签名需要上传证明文件
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse modifySmsSign(String signName, String signSource, String remark,
                                        List<SignFile> signFileList,String signType) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(MODIFY_SMS_SIGN_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(SIGN_NAME, signName);
        request.putQueryParameter(SIGN_SOURCE, signSource);
        request.putQueryParameter(REMARK, remark);
        request.putQueryParameter(SIGN_TYPE, signType);
        if(signFileList != null) {
            for(int i = 1;i <= signFileList.size();i++) {
                request.putQueryParameter("SignFileList." + i + ".FileSuffix", signFileList.get(i - 1).getFileSuffix());
                request.putQueryParameter("SignFileList." + i + ".FileContents", signFileList.get(i - 1).getFileContents());
            }
        }
        return client.getCommonResponse(request);
    }

    /**
     * 删除短信签名
     * @param signName 签名名称
     * @return
     * @throws ServerException
     * @throws ClientException
     */
    public CommonResponse deleteSmsSign(String signName) throws ServerException, ClientException{
        //初始化acsClient,<accessKeyId>和"<accessSecret>"在短信控制台查询即可。
        DefaultProfile profile = DefaultProfile.getProfile(REGIN_ID_VALUE, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(DOMAIN);
        request.setVersion(VERSION);
        request.setAction(DELETE_SMS_SIGN_ACTION);
        request.putQueryParameter(REGIN_ID_NAME, REGIN_ID_VALUE);
        request.putQueryParameter(SIGN_NAME, signName);
        return client.getCommonResponse(request);
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessSecret() {
        return accessSecret;
    }

    public void setAccessSecret(String accessSecret) {
        this.accessSecret = accessSecret;
    }

}

